package com.cloudm.technician.ui.modules.cloudm_box.debug_result

import android.content.Intent
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.cloudm.technician.R
import com.cloudm.technician.data.model.DebugResultInfo
import com.cloudm.technician.data.model.DeviceInfo
import com.cloudm.technician.extensions.openActivity
import com.cloudm.technician.extensions.toast
import com.cloudm.technician.ui.base.BaseMapFragment
import com.cloudm.technician.ui.modules.cloudm_box.map_preview.MapPreviewActivity
import com.cloudm.technician.ui.modules.cloudm_box.upload_pic.UploadPicActivity
import com.cloudm.technician.ui.widget.OnceClickListener
import kotlinx.android.synthetic.main.fragment_debug_result.*

/**
 * 作者: wesley
 * 时间: 2017/12/21
 * 描述: ui
 */
class DebugResultFragment : BaseMapFragment<DebugResultPresenter>(), DebugResultContract.View {

    private var info: DeviceInfo? = null
    private var sn: String = "-1"
    private var deviceId: Int = -1
    private var recordId: String? = null
    private var location: String = ""
    private var isLoaded = false

    private val adapter: DebugResultAdapter by lazy {
        DebugResultAdapter()
    }

    override val resource: Int
        get() = R.layout.fragment_debug_result


    companion object {
        fun newInstance(info: DeviceInfo?) = DebugResultFragment().apply {
            arguments = Bundle().apply {
                putParcelable("info", info)
            }
        }
    }

    override fun initInject() {
        DaggerDebugResultComponent.builder().appComponent(appComponent).debugResultPresenterModule(
                DebugResultPresenterModule(this)).build().inject(this)
    }

    override fun handleBundle(bundle: Bundle) {
        info = bundle.getParcelable("info")
    }

    override fun initVariables() {
        sn = info?.sn ?: "-1"
        deviceId = info?.id ?: -1
        recordId = info?.recordId
    }

    override fun initViews(view: View) {
        snCodeValue.text = sn
        recyclerView.layoutManager = LinearLayoutManager(mContext)
        recyclerView.setHasFixedSize(true)
        recyclerView.adapter = adapter
        super.initViews(view)
        presenter?.countDown()
    }

    override fun locationSuccess() {
        isLoaded = true
        presenter?.getDebugResultInfo(deviceId, sn, lng, lat, recordId)
    }

    override fun initListener() {
        btnDebugResult.setOnClickListener(object : OnceClickListener() {
            override fun onOnceClick(view: View) {
                presenter?.openActivityOrDebug(btnDebugResult.tag.toString(), deviceId, sn, lng,
                        lat, recordId)
            }
        })

        rlLocation.setOnClickListener(object : OnceClickListener() {
            override fun onOnceClick(view: View) {
                presenter?.onLocationClick(rlLocation.tag.toString())
            }
        })
    }

    override fun loadSuccess(info: DebugResultInfo) {
        adapter.addAllItems(info.debugDataList)
        when (info.position?.realPosition) {
            null, "" -> {
                tvLocationValue.text = mContext.resources.getString(
                        R.string.cannot_load_locaiton_msg)
                rlLocation.tag = "0"
            }
            else -> {
                tvLocationValue.text = info.position.realPosition
                lat = info.position.lat ?: 0.0
                lng = info.position.lng ?: 0.0
                location = info.position.realPosition
                rlLocation.tag = "1"
            }
        }
    }

    override fun loadFailure(errorMsg: String) {
        mContext.toast(errorMsg)
    }

    override fun setBtnEnabled() {
        btnDebugResult.isEnabled = true
        btnDebugResult.setBackgroundResource(R.drawable.drawable_button)
    }

    override fun setBtnNotEnabled() {
        btnDebugResult.isEnabled = false
        btnDebugResult.setBackgroundResource(R.drawable.drawable_button_alpha)
    }

    override fun setBtnTextDebug() {
        btnDebugResult.tag = "0"
        btnDebugResult.text = mContext.resources.getString(R.string.debuging)
        btnDebugResult.setTextColor(ContextCompat.getColor(mContext, R.color.color_white_alpha_80))
    }

    override fun setBtnTextNotPass() {
        btnDebugResult.tag = "0"
        btnDebugResult.text = mContext.resources.getString(R.string.debug_not_pass)
        btnDebugResult.setTextColor(ContextCompat.getColor(mContext, R.color.common_color_ffffff))
    }

    override fun setBtnTextNext() {
        btnDebugResult.tag = "1"
        btnDebugResult.text = mContext.resources.getString(R.string.debug_pass)
        btnDebugResult.setTextColor(ContextCompat.getColor(mContext, R.color.common_color_ffffff))
    }

    override fun showDebugInfoEmpMsg() {
        mContext.toast(mContext.resources.getString(R.string.debug_result_is_emp))
    }

    override fun finishSelf() {
        val activity = mContext as? DebugResultActivity ?: return
        activity.finish()
    }

    override fun openUploadPicActivity() {
        val intent = Intent(mContext, UploadPicActivity::class.java).apply {
            putExtras(Bundle().apply {
                putParcelable("info", info)
            })
        }
        mContext.openActivity(intent)
    }

    override fun showLocationEmpMsg() {
        mContext.toast(mContext.resources.getString(R.string.cannot_load_locaiton_msg))
    }

    override fun openMapPreviewActivity() {
        val intent = Intent(mContext, MapPreviewActivity::class.java).apply {
            putExtras(Bundle().apply {
                putDouble("lat", lat)
                putDouble("lng", lng)
                putString("location", location)
            })
        }
        mContext.openActivity(intent, false)
    }


    override fun isLocationLoaded(): Boolean {
        return isLoaded
    }

    override fun reloadLocation() {
        locationSuccess()
    }
}